package com.mofang.powerdekorhelper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewAdapter;
import com.mofang.powerdekorhelper.base.recycleview.BaseViewHolder;
import com.mofang.powerdekorhelper.model.ActivityProcess;
import com.mofang.powerdekorhelper.model.GiftData;
import com.mofang.powerdekorhelper.utils.L;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luofei on 2017/8/31 0031.
 * 说明：
 */

public class ActivityCuponAdapter extends BaseRecycleViewAdapter {

    private List<ActivityProcess.Egggift> egggifts;

    private List<ActivityProcess.Ticketgift> ticketgifts;

    private IListOnclick iListOnclick;

    private int enterFlag;

    GiftData mGiftData;

    public void setEnterFlag(int enterFlag) {
        this.enterFlag = enterFlag;
    }

    public ActivityCuponAdapter(List<?> data, Context mContext, int layoutId) {
        super(data, mContext, layoutId);
    }

    public void setRule(List<ActivityProcess.Egggift> egggifts, List<ActivityProcess.Ticketgift> ticketgifts) {
        this.egggifts = egggifts;
        this.ticketgifts = ticketgifts;
    }

    @Override
    protected <T> void convert(BaseViewHolder holder, T bean, int position) {

        final ActivityProcess.Itmes coupons = (ActivityProcess.Itmes) bean;

        holder.setText(R.id.cupons_code, "兑换码: " + coupons.getCode());

        RelativeLayout continer = (RelativeLayout) holder.itemView.findViewById(R.id.cupons_continer);

        final Spinner giftSpinner = (Spinner) holder.itemView.findViewById(R.id.cupons_gift_spinner);

        if (coupons.getStatus() != null) {

            if (coupons.getStatus() == 1) {

                // ==================>已核销
                continer.setBackgroundResource(R.mipmap.activity_coupons_gray);
                holder.setViewVisiable(R.id.cupons_off_state, View.VISIBLE);
                holder.setViewVisiable(R.id.cupons_effective_state, View.GONE);

                holder.setTextColor(R.id.cupons_off_tv, R.color.fontcolorlight);

                // 核销按钮状态
                holder.setTextBackground(R.id.cupons_off_tv, R.drawable.bg_with_border);
                holder.setViewVisiable(R.id.cupons_off_tv, View.VISIBLE);
                holder.setViewVisiable(R.id.user_card_tv, View.GONE);
                holder.setText(R.id.cupons_off_tv, "已核销");

                // 核销的礼品
                List<String> mList = new ArrayList<>();
                if (coupons.getGift() != null) {
                    mList.add(coupons.getGift());
                } else {
                    mList.add("无");
                }
                final ArrayAdapter<String> sPadapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, mList);
                sPadapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
                giftSpinner.setAdapter(sPadapter);

                if (coupons.getItem_id() == 4) {
                    // 砸蛋规则
                    holder.setText(R.id.cupons_title_flag1, "每满");
                    holder.setText(R.id.cupons_title_flag2, "元，可砸蛋一次");
                    if (coupons.getUse_money() != 0) {
                        holder.setText(R.id.cupons_title, (int) coupons.getUse_money() + "");
                    } else {
                        holder.setText(R.id.cupons_title, "3000");
                    }

                } else {
                    // 抽奖规则
                    holder.setText(R.id.cupons_title_flag1, "每达到");
                    holder.setText(R.id.cupons_title_flag2, "元，可抽奖一次");
                    if (coupons.getUse_money() != 0) {
                        holder.setText(R.id.cupons_title, (int) coupons.getUse_money() + "");
                    } else {
                        holder.setText(R.id.cupons_title, "3000");
                    }
                }

            } else if (coupons.getStatus() == 3) {

                // ==================>已使用

                continer.setBackgroundResource(R.mipmap.activity_coupons_gray);
                holder.setViewVisiable(R.id.cupons_effective_state, View.GONE);
                holder.setViewVisiable(R.id.cupons_off_state, View.GONE);

                // 核销按钮状态
                holder.setTextColor(R.id.cupons_off_tv, R.color.fontcolorlight);
                holder.setTextBackground(R.id.cupons_off_tv, R.drawable.bg_with_border);
                holder.setViewVisiable(R.id.cupons_off_tv, View.VISIBLE);
                holder.setViewVisiable(R.id.user_card_tv, View.GONE);

                holder.setText(R.id.cupons_off_tv, "点击核销");

                // 核销的礼品
                List<String> mList = new ArrayList<>();
                if (coupons.getGift() != null) {
                    mList.add(coupons.getGift());
                } else {
                    mList.add("无");
                }
                final ArrayAdapter<String> sPadapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, mList);
                sPadapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
                giftSpinner.setAdapter(sPadapter);

                if (coupons.getItem_id() == 4) {
                    // 砸蛋规则
                    holder.setText(R.id.cupons_title_flag1, "每满");
                    holder.setText(R.id.cupons_title_flag2, "元，可砸蛋一次");
                    if (coupons.getUse_money() != 0) {
                        holder.setText(R.id.cupons_title, (int) coupons.getUse_money() + "");
                    } else {
                        holder.setText(R.id.cupons_title, "3000");
                    }

                } else {
                    // 抽奖规则
                    holder.setText(R.id.cupons_title_flag1, "每达到");
                    holder.setText(R.id.cupons_title_flag2, "元，可抽奖一次");
                    if (coupons.getUse_money() != 0) {
                        holder.setText(R.id.cupons_title, (int) coupons.getUse_money() + "");
                    } else {
                        holder.setText(R.id.cupons_title, "3000");
                    }
                }

                // 点击核销事件
                holder.setOnclickListener(R.id.cupons_off_tv, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        L.i("ProcessPersenter", "cupons_off_tv");

                        String gift = null;
                        String giftGrade = null;
                        if (giftSpinner.getAdapter().getCount() > 0) {
                            if (mGiftData != null) {
                                gift = mGiftData.getName();
                                giftGrade = mGiftData.getGrade();
                                if (mGiftData.getId() == 0) {
                                    Toast.makeText(mContext, "请选择礼品", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            } else {
                                Toast.makeText(mContext, "请选择礼品", Toast.LENGTH_SHORT).show();
                                return;
                            }

                        } else {
                            if (mGiftData != null) {
                                Toast.makeText(mContext, "请选择礼品", Toast.LENGTH_SHORT).show();
                                return;
                            } else {
                                Toast.makeText(mContext, "请选择礼品", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            //gift = "无";
                        }
                        int giftId = mGiftData.getId();

                        // 核销
                        String giftAndGrade = "";
                        if (gift == null || giftGrade == null) {
                            if (gift == null) {
                                gift = "";
                                giftAndGrade = giftGrade;
                            }
                            if (giftGrade == null) {
                                giftGrade = "";
                                giftAndGrade = gift;
                            }
                        } else {
                            giftAndGrade = giftGrade + "" + gift;
                        }

                        iListOnclick.offCoupons(coupons.getId(), giftId, giftAndGrade, 1);

                        mGiftData = null;
                    }
                });

                //////////////////////////////////////////////////////////////////

                final List<GiftData> giftDatas = new ArrayList<>();
                if (coupons.getItem_id() == 4) {
                    if (egggifts != null) {
                        GiftData data1 = new GiftData();
                        data1.setName("请选择");
                        data1.setId(0);
                        giftDatas.add(data1);
                        for (int i = 0; i < egggifts.size(); i++) {
                            GiftData data = new GiftData();
                            data.setName(egggifts.get(i).getName());
                            data.setGrade(egggifts.get(i).getGrade());
                            data.setId(egggifts.get(i).getId());
                            giftDatas.add(data);
                        }
                    } else {
                        List<String> mList1 = new ArrayList<>();
                        mList.add("无");
                        final ArrayAdapter<String> sPadapter1 = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, mList);
                        sPadapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
                        giftSpinner.setAdapter(sPadapter1);
                    }

                } else {
                    // 礼品
                    if (ticketgifts != null) {
                        GiftData data1 = new GiftData();
                        data1.setName("请选择");
                        data1.setId(0);
                        giftDatas.add(data1);
                        for (int i = 0; i < ticketgifts.size(); i++) {
                            GiftData data = new GiftData();
                            data.setName(ticketgifts.get(i).getName());
                            data.setGrade(ticketgifts.get(i).getGrade());
                            data.setId(ticketgifts.get(i).getId());
                            giftDatas.add(data);
                        }
                    } else {
                        List<String> mList2 = new ArrayList<>();
                        mList.add("无");
                        final ArrayAdapter<String> sPadapter2 = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, mList);
                        sPadapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
                        giftSpinner.setAdapter(sPadapter2);
                    }
                }

                // 设置礼物 giftDatas
                MyAdapter adapter = new MyAdapter(mContext, giftDatas);
                giftSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        mGiftData = giftDatas.get(position);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                giftSpinner.setAdapter(adapter);


            } else {

                // ==================> 活动券

                continer.setBackgroundResource(R.mipmap.activity_coupons_red);
                holder.setViewVisiable(R.id.cupons_off_state, View.GONE);
                holder.setViewVisiable(R.id.cupons_effective_state, View.VISIBLE);

                //holder.setTextColor(R.id.cupons_off_tv, R.color.fontcolorpowerdekor);
                //holder.setTextBackground(R.id.cupons_off_tv, R.drawable.bg_with_border_green);

                holder.setViewVisiable(R.id.cupons_off_tv, View.GONE);

                holder.setViewVisiable(R.id.user_card_tv, View.VISIBLE);

                final List<GiftData> giftDatas = new ArrayList<>();

                if (coupons.getItem_id() == 4) {
                    // 砸蛋
                    if (egggifts != null) {
                        GiftData data1 = new GiftData();
                        data1.setName("请选择");
                        data1.setId(0);
                        giftDatas.add(data1);
                        for (int i = 0; i < egggifts.size(); i++) {
                            GiftData data = new GiftData();
                            data.setName(egggifts.get(i).getName());
                            data.setGrade(egggifts.get(i).getGrade());
                            data.setId(egggifts.get(i).getId());
                            giftDatas.add(data);
                        }
                    } else {
                        List<String> mList = new ArrayList<>();
                        mList.add("无");
                        final ArrayAdapter<String> sPadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, mList);
                        sPadapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
                        giftSpinner.setAdapter(sPadapter);
                    }

                    // 砸蛋规则
                    holder.setText(R.id.cupons_title_flag1, "每满");
                    holder.setText(R.id.cupons_title_flag2, "元，可砸蛋一次");
                    if (coupons.getUse_money() != 0) {
                        holder.setText(R.id.cupons_title, (int) coupons.getUse_money() + "");
                    } else {
                        holder.setText(R.id.cupons_title, "3000");
                    }

                } else {
                    // 礼品
                    if (ticketgifts != null) {
                        GiftData data1 = new GiftData();
                        data1.setName("请选择");
                        data1.setId(0);
                        giftDatas.add(data1);
                        for (int i = 0; i < ticketgifts.size(); i++) {
                            GiftData data = new GiftData();
                            data.setName(ticketgifts.get(i).getName());
                            data.setGrade(ticketgifts.get(i).getGrade());
                            data.setId(ticketgifts.get(i).getId());
                            giftDatas.add(data);
                        }
                    } else {
                        List<String> mList = new ArrayList<>();
                        mList.add("无");
                        final ArrayAdapter<String> sPadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, mList);
                        sPadapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
                        giftSpinner.setAdapter(sPadapter);
                    }

                    // 抽奖规则
                    holder.setText(R.id.cupons_title_flag1, "每达到");
                    holder.setText(R.id.cupons_title_flag2, "元，可抽奖一次");
                    if (coupons.getUse_money() != 0) {
                        holder.setText(R.id.cupons_title, (int) coupons.getUse_money() + "");
                    } else {
                        holder.setText(R.id.cupons_title, "3000");
                    }
                }

                // 点击核销事件（原）
                // 点击使用事件

                holder.setOnclickListener(R.id.user_card_tv, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        iListOnclick.userCard(coupons.getId(), 3);
                    }
                });

                // 设置礼物 giftDatas
                MyAdapter adapter = new MyAdapter(mContext, giftDatas);
                giftSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        mGiftData = giftDatas.get(position);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                giftSpinner.setAdapter(adapter);
            }
        }

        if (enterFlag == 1) {

            holder.setViewVisiable(R.id.cupons_off_tv, View.GONE);
            holder.setViewVisiable(R.id.cupons_gift_spinner, View.GONE);
            holder.setViewVisiable(R.id.cupons_flag_tv, View.GONE);
            holder.setViewVisiable(R.id.user_card_tv, View.GONE);
            if (coupons.getGift() != null) {
                if (coupons.getGrade() != null) {
                    holder.setText(R.id.cupons_gift, "礼品: " + coupons.getGrade() + ": " + coupons.getGift());
                } else {
                    holder.setText(R.id.cupons_gift, "礼品: " + coupons.getGift());
                }
            } else {
                holder.setText(R.id.cupons_gift, "礼品: " + "无");
            }
            holder.setViewVisiable(R.id.cupons_gift, View.VISIBLE);

            holder.setText(R.id.cupons_title, (int) coupons.getUse_money() + "");

        } else {

            holder.setText(R.id.cupons_flag_tv, "对应礼品: ");
            holder.setViewVisiable(R.id.cupons_gift, View.GONE);
        }
    }

    public void setiListOnclick(IListOnclick iListOnclick) {
        this.iListOnclick = iListOnclick;
    }

    public interface IListOnclick {

        void offCoupons(Integer couponId, Integer giftId, String gift, Integer status);

        void userCard(Integer couponId, Integer status);
    }


    class MyAdapter extends BaseAdapter {

        private List<GiftData> giftDatas;
        private Context mContext;

        public MyAdapter(Context mContext, List<GiftData> giftDatas) {
            this.giftDatas = giftDatas;
            this.mContext = mContext;
        }

        @Override
        public int getCount() {
            return giftDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return giftDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            TextView textView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.text_layout, null);

            String grade = giftDatas.get(position).getGrade();
            String name = giftDatas.get(position).getName();

            if (name == null) {
                name = "无";
            }
            if (grade != null) {

                textView.setText(grade + " " + name);
            } else {

                textView.setText(name);
            }

            return textView;
        }
    }
}
